import 'package:flutter/material.dart';
import 'package:flutter_quill/flutter_quill.dart';

class CustomAttributeBuilder extends EmbedBuilder {
  @override
  String get key => 'customAttribute';

  @override
  Widget build(
    BuildContext context,
    EmbedContext embedContext,
  ) {
    final node = embedContext.node;
    final attributeKey = node.value.data['key'] as String;
    final attributeValue = node.value.data['value'];

    // 根据不同的属性类型返回不同的 Widget
    switch (attributeKey) {
      case 'hashtag':
        return Container(
          padding: const EdgeInsets.symmetric(horizontal: 4),
          decoration: BoxDecoration(
            color: Colors.orange[100],
            borderRadius: BorderRadius.circular(4),
          ),
          child: Text(
            '#$attributeValue',
            style: const TextStyle(color: Colors.deepOrange),
          ),
        );
      case 'mention':
        return Container(
          padding: const EdgeInsets.symmetric(horizontal: 4),
          decoration: BoxDecoration(
            color: Colors.blue[100],
            borderRadius: BorderRadius.circular(4),
          ),
          child: Text(
            '@$attributeValue',
            style: const TextStyle(color: Colors.blue),
          ),
        );
      case 'customBackground':
        // 解析颜色值
        final color = Color(int.parse(attributeValue.replaceFirst('#', '0xFF')));
        return Container(
          padding: const EdgeInsets.symmetric(horizontal: 2),
          decoration: BoxDecoration(
            color: color,
            borderRadius: BorderRadius.circular(2),
          ),
          child: embedContext.child,
        );
      default:
        return embedContext.child ?? const SizedBox();
    }
  }

  @override
  String toPlainText(Embed node) {
    final data = node.value.data;
    final key = data['key'] as String;
    final value = data['value'];
    
    switch (key) {
      case 'hashtag':
        return '#$value';
      case 'mention':
        return '@$value';
      default:
        return value.toString();
    }
  }
}